package com.feonix.common.util;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.patchca.background.BackgroundFactory;
import org.patchca.color.SingleColorFactory;
import org.patchca.filter.FilterFactory;
import org.patchca.filter.predefined.CurvesRippleFilterFactory;
import org.patchca.filter.predefined.DoubleRippleFilterFactory;
import org.patchca.service.ConfigurableCaptchaService;
import org.patchca.utils.encoder.EncoderHelper;
import org.patchca.word.RandomWordFactory;

public class VerifyCode {

	private static Log logger = LogFactory.getLog(VerifyCode.class);

	public static String generate(HttpServletResponse httpServletResponse, String sessionName) {
		return generate(httpServletResponse, sessionName, true);
	}

	public static String generate(HttpServletResponse httpServletResponse, String sessionName, boolean curve) {
		HttpServletRequest httpServletRequest = SpringUtil.getRequest();
		HttpSession httpSession = httpServletRequest.getSession(true);
		int i = NumberUtil.getInt(httpServletRequest.getParameter("width"), 110);
		int j = NumberUtil.getInt(httpServletRequest.getParameter("height"), 40);

		SingleColorFactory singleColorFactory = new SingleColorFactory(new Color(25, 60, 170));

		BackgroundFactory backgroundFactory = new BackgroundFactory() {
			public void fillBackground(BufferedImage paramBufferedImage) {
				Graphics graphics = paramBufferedImage.getGraphics();
				graphics.setColor(new Color(252, 252, 252));
			}
		};
		RandomWordFactory randomWordFactory = new RandomWordFactory();

		randomWordFactory.setCharacters("234578ABCEFGHJKLMNPQRSTUVWXYZacdefhjkmnprstuvwxyz");

		ConfigurableCaptchaService configurableCaptchaService = new ConfigurableCaptchaService();
		configurableCaptchaService.setWordFactory(randomWordFactory);

		configurableCaptchaService.setColorFactory(singleColorFactory);
		configurableCaptchaService.setBackgroundFactory(backgroundFactory);
		try {
			configurableCaptchaService.setWidth(i * 2);
			configurableCaptchaService.setHeight(j * 2);
			httpServletResponse.setContentType("image/png");
			httpServletResponse.setHeader("cache", "no-cache");
			randomWordFactory.setMaxLength(4);
			randomWordFactory.setMinLength(4);
			ServletOutputStream servletOutputStream = httpServletResponse.getOutputStream();
			if (curve) {
				CurvesRippleFilterFactory curvesRippleFilterFactory = new CurvesRippleFilterFactory(singleColorFactory);
				curvesRippleFilterFactory.setColorFactory(singleColorFactory);
				configurableCaptchaService.setFilterFactory(curvesRippleFilterFactory);
			} else {
				configurableCaptchaService.setFilterFactory(new DoubleRippleFilterFactory());
			}
			Object obj = EncoderHelper.getChallangeAndWriteImage(configurableCaptchaService, "png",
					servletOutputStream);
			httpSession.setAttribute(sessionName, ((String) obj).toLowerCase());
			servletOutputStream.flush();
			servletOutputStream.close();
		} catch (Exception exception) {
			if ((exception != null) && (StringUtil.contains(exception.getClass().getName(), "ClientAbortException")))
				logger.error("client abort visit");
			else {
				logger.error("generate VerifyCode error", exception);
			}
		}
		return null;
	}
}
